package at.htlkaindorf.jsf.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.htlkaindorf.jsf.application.DataBean;
import at.htlkaindorf.jsf.domain.Entry;
import at.htlkaindorf.jsf.session.SessionBean;

/**
 * @author david.tomes
 */
@ManagedBean(name = "guestbookBean")
@ViewScoped
public class GuestbookBean implements Serializable {

    private static final long serialVersionUID = 949960174551280749L;

    private final Logger logger = LoggerFactory.getLogger(GuestbookBean.class);

    @ManagedProperty(value = "#{dataBean}")
    private DataBean dataBean;

    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    private List<Entry> guestbookEntries = new ArrayList<Entry>();

    @PostConstruct
    public void init() {
        if (sessionBean.getUsername() == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("guestbookLogin.xhtml");
            } catch (IOException e) {
                logger.error(null, e);
            }
        }
        guestbookEntries = dataBean.getGuestbookEntries();
    }

    public DataBean getDataBean() {
        return dataBean;
    }

    public void setDataBean(DataBean dataBean) {
        this.dataBean = dataBean;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public List<Entry> getGuestbookEntries() {
        return guestbookEntries;
    }

    public void setGuestbookEntries(List<Entry> guestbookEntries) {
        this.guestbookEntries = guestbookEntries;
    }
}
